home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / make-367.lha / make-3.67 / make.info-6 (.txt) < prev    next >
GNU Info File  |  1993-05-16  |  49KB  |  877 lines

  1. This is Info file make.info, produced by Makeinfo-1.54 from the input
  2. file make.texinfo.
  3.    This file documents the GNU Make utility, which determines
  4. automatically which pieces of a large program need to be recompiled,
  5. and issues the commands to recompile them.
  6.    This is Edition 0.42, last updated 14 May 1993, of `The GNU Make
  7. Manual', for `make', Version 3.66 Beta.
  8.    Copyright (C) 1988, '89, '90, '91, '92, '93 Free Software
  9. Foundation, Inc.
  10.    Permission is granted to make and distribute verbatim copies of this
  11. manual provided the copyright notice and this permission notice are
  12. preserved on all copies.
  13.    Permission is granted to copy and distribute modified versions of
  14. this manual under the conditions for verbatim copying, provided also
  15. that the section entitled "GNU General Public License" is included
  16. exactly as in the original, and provided that the entire resulting
  17. derived work is distributed under the terms of a permission notice
  18. identical to this one.
  19.    Permission is granted to copy and distribute translations of this
  20. manual into another language, under the above conditions for modified
  21. versions, except that the text of the translations of the section
  22. entitled "GNU General Public License" must be approved for accuracy by
  23. the Foundation.
  24. File: make.info,  Node: Automatic,  Next: Pattern Match,  Prev: Pattern Examples,  Up: Pattern Rules
  25. Automatic Variables
  26. -------------------
  27.    Suppose you are writing a pattern rule to compile a `.c' file into a
  28. `.o' file: how do you write the `cc' command so that it operates on the
  29. right source file name?  You cannot write the name in the command,
  30. because the name is different each time the implicit rule is applied.
  31.    What you do is use a special feature of `make', the "automatic
  32. variables".  These variables have values computed afresh for each rule
  33. that is executed, based on the target and dependencies of the rule.  In
  34. this example, you would use `$@' for the object file name and `$<' for
  35. the source file name.
  36.    Here is a table of automatic variables:
  37.      The file name of the target of the rule.  If the target is an
  38.      archive member, then `$@' is the name of the archive file.  In a
  39.      pattern rule that has multiple targets (*note Introduction to
  40.      Pattern Rules: Pattern Intro.), `$@' is the name of whichever
  41.      target caused the rule's commands to be run.
  42.      The target member name, when the target is an archive member.
  43.      *Note Archives::.  For example, if the target is `foo.a(bar.o)'
  44.      then `$%' is `bar.o' and `$@' is `foo.a'.  `$%' is empty when the
  45.      target is not an archive member.
  46.      The name of the first dependency.  If the target got its commands
  47.      from an implicit rule, this will be the first dependency added by
  48.      the implicit rule (*note Implicit Rules::.).
  49.      The names of all the dependencies that are newer than the target,
  50.      with spaces between them.  For dependencies which are archive
  51.      members, only the member named is used (*note Archives::.).
  52.      The names of all the dependencies, with spaces between them.  For
  53.      dependencies which are archive members, only the member named is
  54.      used (*note Archives::.).
  55.      The stem with which an implicit rule matches (*note How Patterns
  56.      Match: Pattern Match.).  If the target is `dir/a.foo.b' and the
  57.      target pattern is `a.%.b' then the stem is `dir/foo'.  The stem is
  58.      useful for constructing names of related files.
  59.      In a static pattern rule, the stem is part of the file name that
  60.      matched the `%' in the target pattern.
  61.      In an explicit rule, there is no stem; so `$*' cannot be determined
  62.      in that way.  Instead, if the target name ends with a recognized
  63.      suffix (*note Old-Fashioned Suffix Rules: Suffix Rules.), `$*' is
  64.      set to the target name minus the suffix.  For example, if the
  65.      target name is `foo.c', then `$*' is set to `foo', since `.c' is a
  66.      suffix.  GNU `make' does this bizarre thing only for compatibility
  67.      with other implementations of `make'.  You should generally never
  68.      use `$*' except in implicit rules or static pattern rules.
  69.      If the target name in an explicit rule does not end with a
  70.      recognized suffix, `$*' is set to the empty string for that rule.
  71.    `$?' is useful even in explicit rules when you wish to operate on
  72. only the dependencies that have changed.  For example, suppose that an
  73. archive named `lib' is supposed to contain copies of several object
  74. files.  This rule copies just the changed object files into the archive:
  75.      lib: foo.o bar.o lose.o win.o
  76.              ar r lib $?
  77.    Of the variables listed above, four have values that are single file
  78. names, and two have values that are lists of file names.  These six
  79. have variants that get just the file's directory name or just the file
  80. name within the directory.  The variant variables' names are formed by
  81. appending `D' or `F', respectively.  These variants are semi-obsolete
  82. in GNU `make' since the functions `dir' and `notdir' can be used to get
  83. an equivalent effect (*note Functions for File Names: Filename
  84. Functions.).  Here is a table of the variants:
  85. `$(@D)'
  86.      The directory part of the file name of the target.  If the value of
  87.      `$@' is `dir/foo.o' then `$(@D)' is `dir/'.  This value is `./' if
  88.      `$@' does not contain a slash.  `$(@D)' is equivalent to
  89.      `$(dir $@)'.
  90. `$(@F)'
  91.      The file-within-directory part of the file name of the target.  If
  92.      the value of `$@' is `dir/foo.o' then `$(@F)' is `foo.o'.  `$(@F)'
  93.      is equivalent to `$(notdir $@)'.
  94. `$(*D)'
  95. `$(*F)'
  96.      The directory part and the file-within-directory part of the stem;
  97.      `dir/' and `foo' in this example.
  98. `$(%D)'
  99. `$(%F)'
  100.      The directory part and the file-within-directory part of the target
  101.      archive member name.  This makes sense only for archive member
  102.      targets of the form `ARCHIVE(MEMBER)' and is useful only when
  103.      MEMBER may contain a directory name.  (*Note Archive Members as
  104.      Targets: Archive Members.)
  105. `$(<D)'
  106. `$(<F)'
  107.      The directory part and the file-within-directory part of the first
  108.      dependency.
  109. `$(^D)'
  110. `$(^F)'
  111.      Lists of the directory parts and the file-within-directory parts
  112.      of all dependencies.
  113. `$(?D)'
  114. `$(?F)'
  115.      Lists of the directory parts and the file-within-directory parts of
  116.      all dependencies that are newer than the target.
  117.    Note that we use a special stylistic convention when we talk about
  118. these automatic variables; we write "the value of `$<'", rather than
  119. "the variable `<'" as we would write for ordinary variables such as
  120. `objects' and `CFLAGS'.  We think this convention looks more natural in
  121. this special case.  Please do not assume it has a deep significance;
  122. `$<' refers to the variable named `<' just as `$(CFLAGS)' refers to the
  123. variable named `CFLAGS'.  You could just as well use `$(<)' in place of
  124. `$<'.
  125. File: make.info,  Node: Pattern Match,  Next: Match-Anything Rules,  Prev: Automatic,  Up: Pattern Rules
  126. How Patterns Match
  127. ------------------
  128.    A target pattern is composed of a `%' between a prefix and a suffix,
  129. either or both of which may be empty.  The pattern matches a file name
  130. only if the file name starts with the prefix and ends with the suffix,
  131. without overlap.  The text between the prefix and the suffix is called
  132. the "stem".  Thus, when the pattern `%.o' matches the file name
  133. `test.o', the stem is `test'.  The pattern rule dependencies are turned
  134. into actual file names by substituting the stem for the character `%'.
  135. Thus, if in the same example one of the dependencies is written as
  136. `%.c', it expands to `test.c'.
  137.    When the target pattern does not contain a slash (and it usually does
  138. not), directory names in the file names are removed from the file name
  139. before it is compared with the target prefix and suffix.  After the
  140. comparison of the file name to the target pattern, the directory names,
  141. along with the slash that ends them, are added on to the dependency
  142. file names generated from the pattern rule's dependency patterns and
  143. the file name. The directories are ignored only for the purpose of
  144. finding an implicit rule to use, not in the application of that rule.
  145. Thus, `e%t' matches the file name `src/eat', with `src/a' as the stem.
  146. When dependencies are turned into file names, the directories from the
  147. stem are added at the front, while the rest of the stem is substituted
  148. for the `%'.  The stem `src/a' with a dependency pattern `c%r' gives
  149. the file name `src/car'.
  150. File: make.info,  Node: Match-Anything Rules,  Next: Canceling Rules,  Prev: Pattern Match,  Up: Pattern Rules
  151. Match-Anything Pattern Rules
  152. ----------------------------
  153.    When a pattern rule's target is just `%', it matches any file name
  154. whatever.  We call these rules "match-anything" rules.  They are very
  155. useful, but it can take a lot of time for `make' to think about them,
  156. because it must consider every such rule for each file name listed
  157. either as a target or as a dependency.
  158.    Suppose the makefile mentions `foo.c'.  For this target, `make'
  159. would have to consider making it by linking an object file `foo.c.o',
  160. or by C compilation-and-linking in one step from `foo.c.c', or by
  161. Pascal compilation-and-linking from `foo.c.p', and many other
  162. possibilities.
  163.    We know these possibilities are ridiculous since `foo.c' is a C
  164. source file, not an executable.  If `make' did consider these
  165. possibilities, it would ultimately reject them, because files such as
  166. `foo.c.o' and `foo.c.p' would not exist.  But these possibilities are so
  167. numerous that `make' would run very slowly if it had to consider them.
  168.    To gain speed, we have put various constraints on the way `make'
  169. considers match-anything rules.  There are two different constraints
  170. that can be applied, and each time you define a match-anything rule you
  171. must choose one or the other for that rule.
  172.    One choice is to mark the match-anything rule as "terminal" by
  173. defining it with a double colon.  When a rule is terminal, it does not
  174. apply unless its dependencies actually exist.  Dependencies that could
  175. be made with other implicit rules are not good enough.  In other words,
  176. no further chaining is allowed beyond a terminal rule.
  177.    For example, the built-in implicit rules for extracting sources from
  178. RCS and SCCS files are terminal; as a result, if the file `foo.c,v' does
  179. not exist, `make' will not even consider trying to make it as an
  180. intermediate file from `foo.c,v.o' or from `RCS/SCCS/s.foo.c,v'.  RCS
  181. and SCCS files are generally ultimate source files, which should not be
  182. remade from any other files; therefore, `make' can save time by not
  183. looking for ways to remake them.
  184.    If you do not mark the match-anything rule as terminal, then it is
  185. nonterminal.  A nonterminal match-anything rule cannot apply to a file
  186. name that indicates a specific type of data.  A file name indicates a
  187. specific type of data if some non-match-anything implicit rule target
  188. matches it.
  189.    For example, the file name `foo.c' matches the target for the pattern
  190. rule `%.c : %.y' (the rule to run Yacc).  Regardless of whether this
  191. rule is actually applicable (which happens only if there is a file
  192. `foo.y'), the fact that its target matches is enough to prevent
  193. consideration of any nonterminal match-anything rules for the file
  194. `foo.c'.  Thus, `make' will not even consider trying to make `foo.c' as
  195. an executable file from `foo.c.o', `foo.c.c', `foo.c.p', etc.
  196.    The motivation for this constraint is that nonterminal match-anything
  197. rules are used for making files containing specific types of data (such
  198. as executable files) and a file name with a recognized suffix indicates
  199. some other specific type of data (such as a C source file).
  200.    Special built-in dummy pattern rules are provided solely to recognize
  201. certain file names so that nonterminal match-anything rules will not be
  202. considered.  These dummy rules have no dependencies and no commands, and
  203. they are ignored for all other purposes.  For example, the built-in
  204. implicit rule
  205.      %.p :
  206. exists to make sure that Pascal source files such as `foo.p' match a
  207. specific target pattern and thereby prevent time from being wasted
  208. looking for `foo.p.o' or `foo.p.c'.
  209.    Dummy pattern rules such as the one for `%.p' are made for every
  210. suffix listed as valid for use in suffix rules (*note Old-Fashioned
  211. Suffix Rules: Suffix Rules.).
  212. File: make.info,  Node: Canceling Rules,  Prev: Match-Anything Rules,  Up: Pattern Rules
  213. Canceling Implicit Rules
  214. ------------------------
  215.    You can override a built-in implicit rule (or one you have defined
  216. yourself) by defining a new pattern rule with the same target and
  217. dependencies, but different commands.  When the new rule is defined, the
  218. built-in one is replaced.  The new rule's position in the sequence of
  219. implicit rules is determined by where you write the new rule.
  220.    You can cancel a built-in implicit rule by defining a pattern rule
  221. with the same target and dependencies, but no commands.  For example,
  222. the following would cancel the rule that runs the assembler:
  223.      %.o : %.s
  224. File: make.info,  Node: Last Resort,  Next: Suffix Rules,  Prev: Pattern Rules,  Up: Implicit Rules
  225. Defining Last-Resort Default Rules
  226. ==================================
  227.    You can define a last-resort implicit rule by writing a terminal
  228. match-anything pattern rule with no dependencies (*note Match-Anything
  229. Rules::.).  This is just like any other pattern rule; the only thing
  230. special about it is that it will match any target.  So such a rule's
  231. commands are used for all targets and dependencies that have no commands
  232. of their own and for which no other implicit rule applies.
  233.    For example, when testing a makefile, you might not care if the
  234. source files contain real data, only that they exist.  Then you might
  235. do this:
  236.      %::
  237.              touch $@
  238. to cause all the source files needed (as dependencies) to be created
  239. automatically.
  240.    You can instead define commands to be used for targets for which
  241. there are no rules at all, even ones which don't specify commands.  You
  242. do this by writing a rule for the target `.DEFAULT'.  Such a rule's
  243. commands are used for all dependencies which do not appear as targets in
  244. any explicit rule, and for which no implicit rule applies.  Naturally,
  245. there is no `.DEFAULT' rule unless you write one.
  246.    If you use `.DEFAULT' with no commands or dependencies:
  247.      .DEFAULT:
  248. the commands previously stored for `.DEFAULT' are cleared.  Then `make'
  249. acts as if you had never defined `.DEFAULT' at all.
  250.    If you do not want a target to get the commands from a match-anything
  251. pattern rule or `.DEFAULT', but you also do not want any commands to be
  252. run for the target, you can give it empty commands (*note Defining
  253. Empty Commands: Empty Commands.).
  254.    You can use a last-resort rule to override part of another makefile.
  255. *Note Overriding Part of Another Makefile: Overriding Makefiles.
  256. File: make.info,  Node: Suffix Rules,  Next: Search Algorithm,  Prev: Last Resort,  Up: Implicit Rules
  257. Old-Fashioned Suffix Rules
  258. ==========================
  259.    "Suffix rules" are the old-fashioned way of defining implicit rules
  260. for `make'.  Suffix rules are obsolete because pattern rules are more
  261. general and clearer.  They are supported in GNU `make' for
  262. compatibility with old makefiles.  They come in two kinds:
  263. "double-suffix" and "single-suffix".
  264.    A double-suffix rule is defined by a pair of suffixes: the target
  265. suffix and the source suffix.  It matches any file whose name ends with
  266. the target suffix.  The corresponding implicit dependency is made by
  267. replacing the target suffix with the source suffix in the file name.  A
  268. two-suffix rule whose target and source suffixes are `.o' and `.c' is
  269. equivalent to the pattern rule `%.o : %.c'.
  270.    A single-suffix rule is defined by a single suffix, which is the
  271. source suffix.  It matches any file name, and the corresponding implicit
  272. dependency name is made by appending the source suffix.  A single-suffix
  273. rule whose source suffix is `.c' is equivalent to the pattern rule `% :
  274. %.c'.
  275.    Suffix rule definitions are recognized by comparing each rule's
  276. target against a defined list of known suffixes.  When `make' sees a
  277. rule whose target is a known suffix, this rule is considered a
  278. single-suffix rule.  When `make' sees a rule whose target is two known
  279. suffixes concatenated, this rule is taken as a double-suffix rule.
  280.    For example, `.c' and `.o' are both on the default list of known
  281. suffixes.  Therefore, if you define a rule whose target is `.c.o',
  282. `make' takes it to be a double-suffix rule with source suffix `.c' and
  283. target suffix `.o'.  Here is the old-fashioned way to define the rule
  284. for compiling a C source file:
  285.      .c.o:
  286.              $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@ $<
  287.    Suffix rules cannot have any dependencies of their own.  If they
  288. have any, they are treated as normal files with funny names, not as
  289. suffix rules.  Thus, the rule:
  290.      .c.o: foo.h
  291.              $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@ $<
  292. tells how to make the file `.c.o' from the dependency file `foo.h', and
  293. is not at all like the pattern rule:
  294.      %.o: %.c foo.h
  295.              $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@ $<
  296. which tells how to make `.o' files from `.c' files, and makes all `.o'
  297. files using this pattern rule also depend on `foo.h'.
  298.    Suffix rules with no commands are also meaningless.  They do not
  299. remove previous rules as do pattern rules with no commands (*note
  300. Canceling Implicit Rules: Canceling Rules.).  They simply enter the
  301. suffix or pair of suffixes concatenated as a target in the data base.
  302.    The known suffixes are simply the names of the dependencies of the
  303. special target `.SUFFIXES'.  You can add your own suffixes by writing a
  304. rule for `.SUFFIXES' that adds more dependencies, as in:
  305.      .SUFFIXES: .hack .win
  306. which adds `.hack' and `.win' to the end of the list of suffixes.
  307.    If you wish to eliminate the default known suffixes instead of just
  308. adding to them, write a rule for `.SUFFIXES' with no dependencies.  By
  309. special dispensation, this eliminates all existing dependencies of
  310. `.SUFFIXES'.  You can then write another rule to add the suffixes you
  311. want.  For example,
  312.      .SUFFIXES:            # Delete the default suffixes
  313.      .SUFFIXES: .c .o .h   # Define our suffix list
  314.    The `-r' or `--no-builtin-rules' flag causes the default list of
  315. suffixes to be empty.
  316.    The variable `SUFFIXES' is defined to the default list of suffixes
  317. before `make' reads any makefiles.  You can change the list of suffixes
  318. with a rule for the special target `.SUFFIXES', but that does not alter
  319. this variable.
  320. File: make.info,  Node: Search Algorithm,  Prev: Suffix Rules,  Up: Implicit Rules
  321. Implicit Rule Search Algorithm
  322. ==============================
  323.    Here is the procedure `make' uses for searching for an implicit rule
  324. for a target T.  This procedure is followed for each double-colon rule
  325. with no commands, for each target of ordinary rules none of which have
  326. commands, and for each dependency that is not the target of any rule.
  327. It is also followed recursively for dependencies that come from implicit
  328. rules, in the search for a chain of rules.
  329.    Suffix rules are not mentioned in this algorithm because suffix
  330. rules are converted to equivalent pattern rules once the makefiles have
  331. been read in.
  332.    For an archive member target of the form `ARCHIVE(MEMBER)', the
  333. following algorithm is run twice, first using `(MEMBER)' as the target
  334. T, and second using the entire target if the first run found no rule.
  335.   1. Split T into a directory part, called D, and the rest, called N.
  336.      For example, if T is `src/foo.o', then D is `src/' and N is
  337.      `foo.o'.
  338.   2. Make a list of all the pattern rules one of whose targets matches
  339.      T or N.  If the target pattern contains a slash, it is matched
  340.      against T; otherwise, against N.
  341.   3. If any rule in that list is *not* a match-anything rule, then
  342.      remove all nonterminal match-anything rules from the list.
  343.   4. Remove from the list all rules with no commands.
  344.   5. For each pattern rule in the list:
  345.        1. Find the stem S, which is the nonempty part of T or N matched
  346.           by the `%' in the target pattern.
  347.        2. Compute the dependency names by substituting S for `%'; if
  348.           the target pattern does not contain a slash, append D to the
  349.           front of each dependency name.
  350.        3. Test whether all the dependencies exist or ought to exist.
  351.           (If a file name is mentioned in the makefile as a target or
  352.           as an explicit dependency, then we say it ought to exist.)
  353.           If all dependencies exist or ought to exist, or there are no
  354.           dependencies, then this rule applies.
  355.   6. If no pattern rule has been found so far, try harder.  For each
  356.      pattern rule in the list:
  357.        1. If the rule is terminal, ignore it and go on to the next rule.
  358.        2. Compute the dependency names as before.
  359.        3. Test whether all the dependencies exist or ought to exist.
  360.        4. For each dependency that does not exist, follow this algorithm
  361.           recursively to see if the dependency can be made by an
  362.           implicit rule.
  363.        5. If all dependencies exist, ought to exist, or can be made by
  364.           implicit rules, then this rule applies.
  365.   7. If no implicit rule applies, the rule for `.DEFAULT', if any,
  366.      applies.  In that case, give T the same commands that `.DEFAULT'
  367.      has.  Otherwise, there are no commands for T.
  368.    Once a rule that applies has been found, for each target pattern of
  369. the rule other than the one that matched T or N, the `%' in the pattern
  370. is replaced with S and the resultant file name is stored until the
  371. commands to remake the target file T are executed.  After these
  372. commands are executed, each of these stored file names are entered into
  373. the data base and marked as having been updated and having the same
  374. update status as the file T.
  375.    When the commands of a pattern rule are executed for T, the automatic
  376. variables are set corresponding to the target and dependencies.  *Note
  377. Automatic Variables: Automatic.
  378. File: make.info,  Node: Archives,  Next: Features,  Prev: Implicit Rules,  Up: Top
  379. Using `make' to Update Archive Files
  380. ************************************
  381.    "Archive files" are files containing named subfiles called
  382. "members"; they are maintained with the program `ar' and their main use
  383. is as subroutine libraries for linking.
  384. * Menu:
  385. * Archive Members::             Archive members as targets.
  386. * Archive Update::              The implicit rule for archive member targets.
  387. * Archive Suffix Rules::        You can write a special kind of suffix rule
  388.                                   for updating archives.
  389. File: make.info,  Node: Archive Members,  Next: Archive Update,  Up: Archives
  390. Archive Members as Targets
  391. ==========================
  392.    An individual member of an archive file can be used as a target or
  393. dependency in `make'.  The archive file must already exist, but the
  394. member need not exist.  You specify the member named MEMBER in archive
  395. file ARCHIVE as follows:
  396.      ARCHIVE(MEMBER)
  397. This construct is available only in targets and dependencies, not in
  398. commands!  Most programs that you might use in commands do not support
  399. this syntax and cannot act directly on archive members.  Only `ar' and
  400. other programs specifically designed to operate on archives can do so.
  401. Therefore, valid commands to update an archive member target probably
  402. must use `ar'.  For example, this rule says to create a member `hack.o'
  403. in archive `foolib' by copying the file `hack.o':
  404.      foolib(hack.o) : hack.o
  405.              ar r foolib hack.o
  406.    In fact, nearly all archive member targets are updated in just this
  407. way and there is an implicit rule to do it for you.
  408. File: make.info,  Node: Archive Update,  Next: Archive Suffix Rules,  Prev: Archive Members,  Up: Archives
  409. Implicit Rule for Archive Member Targets
  410. ========================================
  411.    Recall that a target that looks like `A(M)' stands for the member
  412. named M in the archive file A.
  413.    When `make' looks for an implicit rule for such a target, as a
  414. special feature it considers implicit rules that match `(M)', as well as
  415. those that match the actual target `A(M)'.
  416.    This causes one special rule whose target is `(%)' to match.  This
  417. rule updates the target `A(M)' by copying the file M into the archive.
  418. For example, it will update the archive member target `foo.a(bar.o)' by
  419. copying the *file* `bar.o' into the archive `foo.a' as a *member* named
  420. `bar.o'.
  421.    When this rule is chained with others, the result is very powerful.
  422. Thus, `make "foo.a(bar.o)"' (the quotes are needed to protect the `('
  423. and `)' from being interpreted specially by the shell) in the presence
  424. of a file `bar.c' is enough to cause the following commands to be run,
  425. even without a makefile:
  426.      cc -c bar.c -o bar.o
  427.      ar r foo.a bar.o
  428.      rm -f bar.o
  429. Here `make' has envisioned the file `bar.o' as an intermediate file.
  430. *Note Chains of Implicit Rules: Chained Rules.
  431.    Implicit rules such as this one are written using the automatic
  432. variable `$%'.  *Note Automatic Variables: Automatic.
  433.    An archive member name in an archive cannot contain a directory
  434. name, but it may be useful in a makefile to pretend that it does.  If
  435. you write an archive member target `foo.a(dir/file.o)', `make' will
  436. perform automatic updating with this command:
  437.      ar r foo.a dir/file.o
  438. which has the effect of copying the file `dir/foo.o' into a member
  439. named `foo.o'.  In connection with such usage, the automatic variables
  440. `%D' and `%F' may be useful.
  441. * Menu:
  442. * Archive Symbols::             How to update archive symbol directories.
  443. File: make.info,  Node: Archive Symbols,  Up: Archive Update
  444. Updating Archive Symbol Directories
  445. -----------------------------------
  446.    An archive file that is used as a library usually contains a special
  447. member named `__.SYMDEF' that contains a directory of the external
  448. symbol names defined by all the other members.  After you update any
  449. other members, you need to update `__.SYMDEF' so that it will summarize
  450. the other members properly.  This is done by running the `ranlib'
  451. program:
  452.      ranlib ARCHIVEFILE
  453.    Normally you would put this command in the rule for the archive file,
  454. and make all the members of the archive file dependencies of that rule.
  455. For example,
  456.      libfoo.a: libfoo.a(x.o) libfoo.a(y.o) ...
  457.              ranlib libfoo.a
  458. The effect of this is to update archive members `x.o', `y.o', etc., and
  459. then update the symbol directory member `__.SYMDEF' by running
  460. `ranlib'.  The rules for updating the members are not shown here; most
  461. likely you can omit them and use the implicit rule which copies files
  462. into the archive, as described in the preceding section.
  463.    This is not necessary when using the GNU `ar' program, which updates
  464. the `__.SYMDEF' member automatically.
  465. File: make.info,  Node: Archive Suffix Rules,  Prev: Archive Update,  Up: Archives
  466. Suffix Rules for Archive Files
  467. ==============================
  468.    You can write a special kind of suffix rule for dealing with archive
  469. files.  *Note Suffix Rules::, for a full explanation of suffix rules.
  470. Archive suffix rules are obsolete in GNU `make', because pattern rules
  471. for archives are a more general mechanism (*note Archive Update::.).
  472. But they are retained for compatibility with other `make's.
  473.    To write a suffix rule for archives, you simply write a suffix rule
  474. using the target suffix `.a' (the usual suffix for archive files).  For
  475. example, here is the old-fashioned suffix rule to update a library
  476. archive from C source files:
  477.      .c.a:
  478.              $(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $*.o
  479.              $(AR) r $@ $*.o
  480.              $(RM) $*.o
  481. This works just as if you had written the pattern rule:
  482.      (%.o): %.c
  483.              $(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $*.o
  484.              $(AR) r $@ $*.o
  485.              $(RM) $*.o
  486.    In fact, this is just what `make' does when it sees a suffix rule
  487. with `.a' as the target suffix.  Any double-suffix rule `.X.a' is
  488. converted to a pattern rule with the target pattern `(%.o)' and a
  489. dependency pattern of `%.X'.
  490. File: make.info,  Node: Features,  Next: Missing,  Prev: Archives,  Up: Top
  491. Features of GNU `make'
  492. **********************
  493.    Here is a summary of the features of GNU `make', for comparison with
  494. and credit to other versions of `make'.  We consider the features of
  495. `make' in 4.2 BSD systems as a baseline.  If you are concerned with
  496. writing portable makefiles, you should use only the features of `make'
  497. *not* listed here or in *Note Missing::.
  498.    Many features come from the version of `make' in System V.
  499.    * The `VPATH' variable and its special meaning.  *Note Searching
  500.      Directories for Dependencies: Directory Search.  This feature
  501.      exists in System V `make', but is undocumented.  It is documented
  502.      in 4.3 BSD `make' (which says it mimics System V's `VPATH'
  503.      feature).
  504.    * Included makefiles.  *Note Including Other Makefiles: Include.
  505.      Allowing multiple files to be included with a single directive is
  506.      a GNU extension.
  507.    * Variables are read from and communicated via the environment.
  508.      *Note Variables from the Environment: Environment.
  509.    * Options passed through the variable `MAKEFLAGS' to recursive
  510.      invocations of `make'.  *Note Communicating Options to a
  511.      Sub-`make': Options/Recursion.
  512.    * The automatic variable `$%' is set to the member name in an
  513.      archive reference.  *Note Automatic Variables: Automatic.
  514.    * The automatic variables `$@', `$*', `$<', `$%', and `$?' have
  515.      corresponding forms like `$(@F)' and `$(@D)'.  We have generalized
  516.      this to `$^' as an obvious extension.  *Note Automatic Variables:
  517.      Automatic.
  518.    * Substitution variable references.  *Note Basics of Variable
  519.      References: Reference.
  520.    * The command-line options `-b' and `-m', accepted and ignored.  In
  521.      System V `make', these options actually do something.
  522.    * Execution of recursive commands to run `make' via the variable
  523.      `MAKE' even if `-n', `-q' or `-t' is specified.  *Note Recursive
  524.      Use of `make': Recursion.
  525.    * Support for suffix `.a' in suffix rules.  *Note Archive Suffix
  526.      Rules::.  This feature is obsolete in GNU `make', because the
  527.      general feature of rule chaining (*note Chains of Implicit Rules:
  528.      Chained Rules.) allows one pattern rule for installing members in
  529.      an archive (*note Archive Update::.) to be sufficient.
  530.    * The arrangement of lines and backslash-newline combinations in
  531.      commands is retained when the commands are printed, so they appear
  532.      as they do in the makefile, except for the stripping of initial
  533.      whitespace.
  534.    The following features were inspired by various other versions of
  535. `make'.  In some cases it is unclear exactly which versions inspired
  536. which others.
  537.    * Pattern rules using `%'.  This has been implemented in several
  538.      versions of `make'.  We're not sure who invented it first, but
  539.      it's been spread around a bit.  *Note Defining and Redefining
  540.      Pattern Rules: Pattern Rules.
  541.    * Rule chaining and implicit intermediate files.  This was
  542.      implemented by Stu Feldman in his version of `make' for AT&T
  543.      Eighth Edition Research Unix, and later by Andrew Hume of AT&T
  544.      Bell Labs in his `mk' program (where he terms it "transitive
  545.      closure").  We do not really know if we got this from either of
  546.      them or thought it up ourselves at the same time.  *Note Chains of
  547.      Implicit Rules: Chained Rules.
  548.    * The automatic variable `$^' containing a list of all dependencies
  549.      of the current target.  We did not invent this, but we have no
  550.      idea who did.  *Note Automatic Variables: Automatic.
  551.    * The "what if" flag (`-W' in GNU `make') was (as far as we know)
  552.      invented by Andrew Hume in `mk'.  *Note Instead of Executing the
  553.      Commands: Instead of Execution.
  554.    * The concept of doing several things at once (parallelism) exists in
  555.      many incarnations of `make' and similar programs, though not in the
  556.      System V or BSD implementations.  *Note Command Execution:
  557.      Execution.
  558.    * Modified variable references using pattern substitution come from
  559.      SunOS 4.0.  *Note Basics of Variable References: Reference.  This
  560.      functionality was provided in GNU `make' by the `patsubst'
  561.      function before the alternate syntax was implemented for
  562.      compatibility with SunOS 4.0.  It is not altogether clear who
  563.      inspired whom, since GNU `make' had `patsubst' before SunOS 4.0
  564.      was released.
  565.    * The special significance of `+' characters preceding command lines
  566.      (*note Instead of Executing the Commands: Instead of Execution.) is
  567.      mandated by draft 11.2 of IEEE Std 1003.2 (POSIX).
  568.    * The `+=' syntax to append to the value of a variable comes from
  569.      SunOS 4.0 `make'.  *Note Appending More Text to Variables:
  570.      Appending.
  571.    The remaining features are inventions new in GNU `make':
  572.    * Use the `-v' or `--version' option to print version and copyright
  573.      information.
  574.    * Use the `-h' or `--help' option to summarize the options to `make'.
  575.    * Simply-expand variables.  *Note The Two Flavors of Variables:
  576.      Flavors.
  577.    * Pass command-line variable assignments automatically through the
  578.      variable `MAKE' to recursive `make' invocations.  *Note Recursive
  579.      Use of `make': Recursion.
  580.    * Use the `-C' or `--directory' command option to change directory.
  581.      *Note Summary of Options: Options Summary.
  582.    * Make verbatim variable definitions with `define'.  *Note Defining
  583.      Variables Verbatim: Defining.
  584.    * Declare phony targets with the special target `.PHONY'.
  585.      Andrew Hume of AT&T Bell Labs implemented a similar feature with a
  586.      different syntax in his `mk' program.  This seems to be a case of
  587.      parallel discovery.  *Note Phony Targets: Phony Targets.
  588.    * Manipulate text by calling functions.  *Note Functions for
  589.      Transforming Text: Functions.
  590.    * Use the `-o' or `--old-file' option to pretend a file's
  591.      modification-time is old.  *Note Avoiding Recompilation of Some
  592.      Files: Avoiding Compilation.
  593.    * Conditional execution.
  594.      This feature has been implemented numerous times in various
  595.      versions of `make'; it seems a natural extension derived from the
  596.      features of the C preprocessor and similar macro languages and is
  597.      not a revolutionary concept.  *Note Conditional Parts of
  598.      Makefiles: Conditionals.
  599.    * Specify the included makefile search path.  *Note Including Other
  600.      Makefiles: Include.
  601.    * Specify extra makefiles to read.  *Note The Variable `MAKEFILES':
  602.      MAKEFILES Variable.
  603.    * Strip leading sequences of `./' from file names, so that `./FILE'
  604.      and `FILE' are considered to be the same file.
  605.    * Use a special search method for library dependencies written in the
  606.      form `-lNAME'.  *Note Directory Search for Link Libraries:
  607.      Libraries/Search.
  608.    * Allow suffixes for suffix rules (*note Old-Fashioned Suffix Rules:
  609.      Suffix Rules.) to contain any characters.  In other version of
  610.      `make', they must begin with `.' and not contain any `/'
  611.      characters.
  612.    * Keep track of the current level of `make' recursion using the
  613.      variable `MAKELEVEL'.  *Note Recursive Use of `make': Recursion.
  614.    * Specify static pattern rules.  *Note Static Pattern Rules: Static
  615.      Pattern.
  616.    * Provide selective `vpath' search.  *Note Searching Directories for
  617.      Dependencies: Directory Search.
  618.    * Provide computed variable references.  *Note Basics of Variable
  619.      References: Reference.
  620.    * Update makefiles.  *Note How Makefiles Are Remade: Remaking
  621.      Makefiles.  System V `make' has a very, very limited form of this
  622.      functionality in that it will check out SCCS files for makefiles.
  623.    * Various new built-in implicit rules.  *Note Catalogue of Implicit
  624.      Rules: Catalogue of Rules.
  625. File: make.info,  Node: Missing,  Next: Makefile Conventions,  Prev: Features,  Up: Top
  626. Incompatibilities and Missing Features
  627. **************************************
  628.    The `make' programs in various other systems support a few features
  629. that are not implemented in GNU `make'.  Draft 11.2 of the POSIX.2
  630. standard which specifies `make' does not require any of these features.
  631.    * A target of the form `FILE((ENTRY))' stands for a member of
  632.      archive file FILE.  The member is chosen, not by name, but by
  633.      being an object file which defines the linker symbol ENTRY.
  634.      This feature was not put into GNU `make' because of the
  635.      nonmodularity of putting knowledge into `make' of the internal
  636.      format of archive file symbol tables.  *Note Updating Archive
  637.      Symbol Directories: Archive Symbols.
  638.    * Suffixes (used in suffix rules) that end with the character `~'
  639.      have a special meaning to System V `make'; they refer to the SCCS
  640.      file that corresponds to the file one would get without the `~'.
  641.      For example, the suffix rule `.c~.o' would make the file `N.o'
  642.      file from the SCCS file `s.N.c'.  For complete coverage, a whole
  643.      series of such suffix rules is required.  *Note Old-Fashioned
  644.      Suffix Rules: Suffix Rules.
  645.      In GNU `make', this entire series of cases is handled by two
  646.      pattern rules for extraction from SCCS, in combination with the
  647.      general feature of rule chaining.  *Note Chains of Implicit Rules:
  648.      Chained Rules.
  649.    * In System V `make', the string `$$@' has the strange meaning that,
  650.      in the dependencies of a rule with multiple targets, it stands for
  651.      the particular target that is being processed.
  652.      This is not defined in GNU `make' because `$$' should always stand
  653.      for an ordinary `$'.
  654.      It is possible to get this functionality through the use of static
  655.      pattern rules (*note Static Pattern Rules: Static Pattern.).  The
  656.      System V `make' rule:
  657.           $(targets): $$@.o lib.a
  658.      can be replaced with the GNU `make' static pattern rule:
  659.           $(targets): %: %.o lib.a
  660.    * In System V and 4.3 BSD `make', files found by `VPATH' search
  661.      (*note Searching Directories for Dependencies: Directory Search.)
  662.      have their names changed inside command strings.  We feel it is
  663.      much cleaner to always use automatic variables and thus make this
  664.      feature obsolete.
  665.    * In some Unix `make's, implicit rule search (*note Using Implicit
  666.      Rules: Implicit Rules.) is apparently done for *all* targets, not
  667.      just those without commands.  This means you can do:
  668.           foo.o:
  669.                   cc -c foo.c
  670.      and Unix `make' will intuit that `foo.o' depends on `foo.c'.
  671.      We feel that such usage is broken.  The dependency properties of
  672.      `make' are well-defined (for GNU `make', at least), and doing such
  673.      a thing simply does not fit the model.
  674.    * GNU `make' does not include any built-in implicit rules for
  675.      compiling or preprocessing EFL programs.  If we hear of anyone who
  676.      is using EFL, we will gladly add them.
  677.    * It appears that in SVR4 `make', a suffix rule can be specified with
  678.      no commands, and it is treated as if it had empty commands (*note
  679.      Empty Commands::.).  For example:
  680.           .c.a:
  681.      will override the built-in `.c.a' suffix rule.
  682.      We feel that it is cleaner for a rule without commands to always
  683.      simply add to the dependency list for the target.  The above
  684.      example can be easily rewritten to get the desired behavior in GNU
  685.      `make':
  686.           .c.a: ;
  687. File: make.info,  Node: Makefile Conventions,  Next: Quick Reference,  Prev: Missing,  Up: Top
  688. Makefile Conventions
  689. ********************
  690.    This chapter describes conventions for writing the Makefiles for GNU
  691. programs.
  692. * Menu:
  693. * Makefile Basics::
  694. * Utilities in Makefiles::
  695. * Standard Targets::
  696. * Command Variables::
  697. * Directory Variables::
  698. File: make.info,  Node: Makefile Basics,  Next: Utilities in Makefiles,  Up: Makefile Conventions
  699. General Conventions for Makefiles
  700. =================================
  701.    Every Makefile should contain this line:
  702.      SHELL = /bin/sh
  703. to avoid trouble on systems where the `SHELL' variable might be
  704. inherited from the environment.  (This is never a problem with GNU
  705. `make'.)
  706.    Don't assume that `.' is in the path for command execution.  When
  707. you need to run programs that are a part of your package during the
  708. make, please make sure that it uses `./' if the program is built as
  709. part of the make or `$(srcdir)/' if the file is an unchanging part of
  710. the source code.  Without one of these prefixes, the current search
  711. path is used.
  712.    The distinction between `./' and `$(srcdir)/' is important when
  713. using the `--srcdir' option to `configure'.  A rule of the form:
  714.      foo.1 : foo.man sedscript
  715.              sed -e sedscript foo.man > foo.1
  716. will fail when the current directory is not the source directory,
  717. because `foo.man' and `sedscript' are not in the current directory.
  718.    When using GNU `make', relying on `VPATH' to find the source file
  719. will work in the case where there is a single dependency file, since
  720. the `make' automatic variable `$<' will represent the source file
  721. wherever it is.  (Many versions of `make' set `$<' only in implicit
  722. rules.)  A makefile target like
  723.      foo.o : bar.c
  724.              $(CC) -I. -I$(srcdir) $(CFLAGS) -c bar.c -o foo.o
  725. should instead be written as
  726.      foo.o : bar.c
  727.              $(CC) $(CFLAGS) $< -o $@
  728. in order to allow `VPATH' to work correctly.  When the target has
  729. multiple dependencies, using an explicit `$(srcdir)' is the easiest way
  730. to make the rule work well.  For example, the target above for `foo.1'
  731. is best written as:
  732.      foo.1 : foo.man sedscript
  733.              sed -s $(srcdir)/sedscript $(srcdir)/foo.man > foo.1
  734. File: make.info,  Node: Utilities in Makefiles,  Next: Standard Targets,  Prev: Makefile Basics,  Up: Makefile Conventions
  735. Utilities in Makefiles
  736. ======================
  737.    Write the Makefile commands (and any shell scripts, such as
  738. `configure') to run in `sh', not in `csh'.  Don't use any special
  739. features of `ksh' or `bash'.
  740.    The `configure' script and the Makefile rules for building and
  741. installation should not use any utilities directly except these:
  742.      cat cmp cp echo egrep expr grep
  743.      ln mkdir mv pwd rm rmdir sed test touch
  744.    Stick to the generally supported options for these programs.  For
  745. example, don't use `mkdir -p', convenient as it may be, because most
  746. systems don't support it.
  747.    The Makefile rules for building and installation can also use
  748. compilers and related programs, but should do so via `make' variables
  749. so that the user can substitute alternatives.  Here are some of the
  750. programs we mean:
  751.      ar bison cc flex install ld lex
  752.      make makeinfo ranlib texi2dvi yacc
  753.    When you use `ranlib', you should test whether it exists, and run it
  754. only if it exists, so that the distribution will work on systems that
  755. don't have `ranlib'.
  756.    If you use symbolic links, you should implement a fallback for
  757. systems that don't have symbolic links.
  758.    It is ok to use other utilities in Makefile portions (or scripts)
  759. intended only for particular systems where you know those utilities to
  760. exist.
  761. File: make.info,  Node: Standard Targets,  Next: Command Variables,  Prev: Utilities in Makefiles,  Up: Makefile Conventions
  762. Standard Targets for Users
  763. ==========================
  764.    All GNU programs should have the following targets in their
  765. Makefiles:
  766. `all'
  767.      Compile the entire program.  This should be the default target.
  768.      This target need not rebuild any documentation files; info files
  769.      should normally be included in the distribution, and DVI files
  770.      should be made only when explicitly asked for.
  771. `install'
  772.      Compile the program and copy the executables, libraries, and so on
  773.      to the file names where they should reside for actual use.  If
  774.      there is a simple test to verify that a program is properly
  775.      installed then run that test.
  776.      Use `-' before any command for installing a man page, so that
  777.      `make' will ignore any errors.  This is in case there are systems
  778.      that don't have the Unix man page documentation system installed.
  779.      In the future, when we have a standard way of installing info
  780.      files, `install' targets will be the proper place to do so.
  781. `uninstall'
  782.      Delete all the installed files that the `install' target would
  783.      create (but not the noninstalled files such as `make all' would
  784.      create).
  785. `clean'
  786.      Delete all files from the current directory that are normally
  787.      created by building the program.  Don't delete the files that
  788.      record the configuration.  Also preserve files that could be made
  789.      by building, but normally aren't because the distribution comes
  790.      with them.
  791.      Delete `.dvi' files here if they are not part of the distribution.
  792. `distclean'
  793.      Delete all files from the current directory that are created by
  794.      configuring or building the program.  If you have unpacked the
  795.      source and built the program without creating any other files,
  796.      `make distclean' should leave only the files that were in the
  797.      distribution.
  798. `mostlyclean'
  799.      Like `clean', but may refrain from deleting a few files that people
  800.      normally don't want to recompile.  For example, the `mostlyclean'
  801.      target for GCC does not delete `libgcc.a', because recompiling it
  802.      is rarely necessary and takes a lot of time.
  803. `realclean'
  804.      Delete everything from the current directory that can be
  805.      reconstructed with this Makefile.  This typically includes
  806.      everything deleted by distclean, plus more: C source files
  807.      produced by Bison, tags tables, info files, and so on.
  808.      One exception, however: `make realclean' should not delete
  809.      `configure' even if `configure' can be remade using a rule in the
  810.      Makefile.  More generally, `make realclean' should not delete
  811.      anything that needs to exist in order to run `configure' and then
  812.      begin to build the program.
  813. `TAGS'
  814.      Update a tags table for this program.
  815. `info'
  816.      Generate any info files needed.  The best way to write the rules
  817.      is as follows:
  818.           info:  foo.info
  819.           
  820.           foo.info: $(srcdir)/foo.texi $(srcdir)/chap1.texi $(srcdir)/chap2.texi
  821.                   $(MAKEINFO) $(srcdir)/foo.texi
  822.      You must define the variable `MAKEINFO' in the Makefile.  It
  823.      should run the Makeinfo program, which is part of the Texinfo2
  824.      distribution.
  825. `dvi'
  826.      Generate DVI files for all TeXinfo documentation.  For example:
  827.           dvi: foo.dvi
  828.           
  829.           foo.dvi: $(srcdir)/foo.texi $(srcdir)/chap1.texi $(srcdir)/chap2.texi
  830.                   $(TEXI2DVI) $(srcdir)/foo.texi
  831.      You must define the variable `TEXI2DVI' in the Makefile.  It should
  832.      run the program `texi2dvi', which is part of the Texinfo2
  833.      distribution.  Alternatively, write just the dependencies, and
  834.      allow GNU Make to provide the command.
  835. `dist'
  836.      Create a distribution tar file for this program.  The tar file
  837.      should be set up so that the file names in the tar file start with
  838.      a subdirectory name which is the name of the package it is a
  839.      distribution for.  This name can include the version number.
  840.      For example, the distribution tar file of GCC version 1.40 unpacks
  841.      into a subdirectory named `gcc-1.40'.
  842.      The easiest way to do this is to create a subdirectory
  843.      appropriately named, use `ln' or `cp' to install the proper files
  844.      in it, and then `tar' that subdirectory.
  845.      The `dist' target should explicitly depend on all non-source files
  846.      that are in the distribution, to make sure they are up to date in
  847.      the distribution.  *Note Making Releases: (standards)Releases.
  848. `check'
  849.      Perform self-tests (if any).  The user must build the program
  850.      before running the tests, but need not install the program; you
  851.      should write the self-tests so that they work when the program is
  852.      built but not installed.
  853.    The following target is suggested as a conventional name, for
  854. programs in which it is useful.
  855. `installcheck'
  856.      Perform installation tests (if any).  The user must build and
  857.      install the program before running the tests.  You should not
  858.      assume that `$(bindir)' is in the search path.
  859. `installdirs'
  860.      It's useful to add a target named `installdirs' to create the
  861.      directories where files are installed, and their parent
  862.      directories.  You can use a rule like this:
  863.           # Make sure all installation directories, e.g. $(bindir) actually exist by
  864.           # making them if necessary.
  865.           installdirs:
  866.                   for file in $(bindir) $(datadir) $(libdir) $(infodir) $(mandir) ; do \
  867.                      oIFS="$${IFS}"; IFS='/'; set - $${file}; IFS="$${oIFS}"; \
  868.                      pathcomp=''; test ".$${1}" = "." && shift; \
  869.                      while test $$# -ne 0 ; do \
  870.                        pathcomp="$${pathcomp}/$${1}"; shift; \
  871.                        if test ! -d "$${pathcomp}"; then \
  872.                           echo "making directory $$pathcomp" 1>&2 ; \
  873.                           mkdir "$${pathcomp}"; \
  874.                        fi; \
  875.                      done; \
  876.                   done
  877.